package sanglh.edu.hsphysics.main;

import java.util.ArrayList;

import sanglh.edu.hsphysics.R;
import sanglh.edu.hsphysics.base.BazeItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<BazeItem> items;
	private int resId;

	public MenuAdapter(Context context, ArrayList<BazeItem> items,
			int resId) {
		super();
		this.context = context;
		this.items = items;
		this.resId = resId;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Hoder hoder = null;
		BazeItem item = items.get(position);
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(resId, null);
			hoder = new Hoder();
			hoder.img = (ImageView) convertView.findViewById(R.id.img);
			hoder.tv = (TextView) convertView.findViewById(R.id.tv);
			convertView.setTag(hoder);
		} else {
			hoder = (Hoder) convertView.getTag();
		}

		hoder.img.setImageResource(item.resIcon);
		hoder.tv.setText(item.title);
		return convertView;
	}

	static class Hoder {
		ImageView img;
		TextView tv;
	}
}
